package gdut.jmxywz.action;

import gdut.jmxywz.been.Info;
import gdut.jmxywz.been.InfoFiles;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
/**
 * 学生党建的affireaction
 * @author Administrator
 *
 */
public class parttyconstructionStudentAffairsAction extends BaseAction {
	
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	
	private Info     info;
	private Teacher  teacher;
	
	private  String   where_search; //查询条件
	private  String   where_value;
	private  String   like ;
	private  String   searchWhere="";//拼接查询条件跳页
	
	
	//文件
	private String[] files_content;
	private String[] files_title;
	private List<File> upload_file;
	private String[] files_save_as;
	private String[] upload_fileContentType;
	private String[] upload_fileFileName;
	private String   savePath;
	private List<InfoFiles>     infoFiles;
	
	
	//分页
	private int        currentPage=1;
	private int        pageSize=5;
	private String     nextPage;
	private PageModel  pm;
	
	
    

	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public Info getInfo() {
		return info;
	}
	public void setInfo(Info info) {
		this.info = info;
	}
	
	public String getWhere_search() {
		return where_search;
	}
	public void setWhere_search(String whereSearch) {
		where_search = whereSearch;
	}
	public String getWhere_value() {
		return where_value;
	}
	public void setWhere_value(String where_value)throws UnsupportedEncodingException  {
	     this.where_value =new String(where_value.getBytes("ISO-8859-1"),"utf-8");
	}
	
	public String getLike() {
		return like;
	}
	public void setLike(String like) {
		this.like = like;
	}
	public String getSearch() {
		return where_search;
	}
	public void setSearch(String where_search) {
		this.where_search = where_search;
	}
	public String getSearchWhere() {
		return searchWhere;
	}
	public void setSearchWhere(String searchWhere) {
		this.searchWhere = searchWhere;
	}
	
	
	public String[] getFiles_content() {
		return files_content;
	}
	public void setFiles_content(String[] filesContent) {
		files_content = filesContent;
	}
	public String[] getFiles_title() {
		return files_title;
	}
	public void setFiles_title(String[] filesTitle) {
		files_title = filesTitle;
	}
	public List<File> getUpload_file() {
		return upload_file;
	}
	public void setUpload_file(List<File> uploadFile) {
		upload_file = uploadFile;
	}
	public String[] getFiles_save_as() {
		return files_save_as;
	}
	public void setFiles_save_as(String[] filesSaveAs) {
		files_save_as = filesSaveAs;
	}
	public String[] getUpload_fileContentType() {
		return upload_fileContentType;
	}
	public void setUpload_fileContentType(String[] uploadFileContentType) {
		upload_fileContentType = uploadFileContentType;
	}
	public String[] getUpload_fileFileName() {
		return upload_fileFileName;
	}
	public void setUpload_fileFileName(String[] uploadFileFileName) {
		upload_fileFileName = uploadFileFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public List<InfoFiles> getInfoFiles() {
		return infoFiles;
	}
	public void setInfoFiles(List<InfoFiles> infoFiles) {
		this.infoFiles = infoFiles;
	}

	
	
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getNextPage() {
		return nextPage;
	}
	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public PageModel getPm() {
		return pm;
	}
	public void setPm(PageModel pm) {
		this.pm = pm;
	}
	
	
	
	/***
	 *  ------------Add 发布信息
	 * 
	 */
	
	public String Add() throws Exception{
   
		teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		
		List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		setSavePath("");//上传路径
		boolean bl=false;
		boolean b2=false;
	
		System.out.println("==================");
		
		System.out.println(info.getInfoTime());
	
		info.setTeacherByInfoEditer(teacher);
		
		//先插入info表
		 //判断信息内容、发布者、标题是否为空，
		if("".equals(info.getInfoTitle())||"".equals(info.getInfoContext())||"".equals(info.getInfoSender())){
			
			return "error";
		}
	
		bl= this.getCommonService().saveObject(info);
		
		//处理文件上传

		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分
				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\affficheFiles\\"+extend_name;//文件在服务器保存的相对路径
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
				InfoFiles afiles=new InfoFiles();
							
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				afiles.setFileUrl(Save_path);
				afiles.setFileName(files_title[i]);
				afiles.setFileContext(files_content[i]);
				afiles.setInfo(info);	
	
				//在插入附件表
				b2=this.getCommonService().saveObject(afiles);
		
		//文件上传处理结束
		
     	}
		
		if(bl)
		{
			return "parttyconstructionStudentAffairsAdd";
		}else{
			return  "error";
		}
		
	}
	
	/***
	 * List 后台最新所有已通过审核的 通知列表 
	 * 
	 */
      public String List() throws Exception{
    	  
    	  String hql =" from Info where  infoType=5 and auditing in(1,2)";
    	  String[] parem = null;
     	  String  search_parameter = "";
  	      String en_where_value="";
  	      
    	  nextPage ="parttyconstructionstudentAffairsList.action" ;
    	  
    	  if(!"".equals(where_value)&&where_value!=null){
    		  
    		  en_where_value=java.net.URLEncoder.encode(where_value,"utf-8");//URL编码转换
    		  
    		  System.out.println("++++-- "+where_value);
    		  if(like.equals("like"))
    		  {
             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
             	   
             	   
    		  }else{
    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
    		  }
    		  
    	  }
    	  
    	  //查询条件
    	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
    	  
    	  hql = hql +" order by infoTime desc,infoId desc";
    	  
    	  pm = new PageModel();
    	  
    	  pm=this.getCommonService().findPage(hql, parem, currentPage, pageSize, nextPage,search_parameter);
    	  
    	  return "parttyconstructionStudentAffairsList";
      }
      
      //
     public String List1() throws Exception{
    	  
    	 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
 		
  	   String hql ="from Info where infoType = 5";
   	   String[] parem =null;
   	   String  search_parameter = "";
   	   String en_where_value="";
   	  
   	  if(!"".equals(where_value)&&where_value!=null){
   		  
   		  en_where_value=java.net.URLEncoder.encode(where_value,"utf-8");//URL编码转换
   		  if(like.equals("like"))
   		  {
   			
   			  System.out.println("fanhuid == "+where_value);
            	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
            	   System.out.println("====++++"+where_value);
            	  
   		  }else{
   			  hql = hql +"and "+where_search+" = '"+where_value+"'";

   		  }
   		  
        	  System.out.println("------------++ "+search_parameter);
   	  }
 
   	  if(info==null||info.getAuditing()==null){
   		  
   		  nextPage ="parttyconstructionstudentAffairsList1.action" ;
   	     
   		  
   		  hql =hql + " order by auditing asc ,infoTime desc, infoId desc ";
   		  
   	  }else
   	    {
   	     hql =hql +"and auditing="+info.getAuditing()+ "order by  auditing  asc ,infoTime desc,infoId desc  ";
   	     nextPage ="parttyconstructionstudentAffairsAuditing.action?info.auditing=0&" ;
   	     
   	    }
   	  System.out.println("=============  "+where_value);
   	  System.out.println("============= "+where_search);
   	  System.out.println("------------- " +like);
   	  

		   search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
   	  
		   System.out.println("---"+search_parameter);
		   System.out.println("---"+hql);

		   pm = new PageModel();
   	  
   	   System.out.println("hql222---"+hql);
   	   
   	   pm=this.getCommonService().findPage(hql,parem, currentPage, pageSize, nextPage,search_parameter);
   	  
   	   System.out.println("---------------------"+pm.getTotalCount());
    	  
    	  return "parttyconstructionStudentAffairsList1";
      }
     
     //
     public String List2() throws Exception{
   	  
    	 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
 		
  	   String hql ="from Info where infoType = 5";
   	   String[] parem =null;
   	   String  search_parameter = "";
   	   String en_where_value="";
   	  
   	  if(!"".equals(where_value)&&where_value!=null){
   		  
   		  en_where_value=java.net.URLEncoder.encode(where_value,"utf-8");//URL编码转换
   		  if(like.equals("like"))
   		  {
   			
   			  System.out.println("fanhuid == "+where_value);
            	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
            	   System.out.println("====++++"+where_value);
            	  
   		  }else{
   			  hql = hql +"and "+where_search+" = '"+where_value+"'";

   		  }
   		  
        	  System.out.println("------------++ "+search_parameter);
   	  }
 
   	  if(info==null||info.getAuditing()==null){
   		  
   		  nextPage ="parttyconstructionstudentAffairsList2.action" ;
   	     
   		  
   		  hql =hql + " order by auditing asc ,infoTime desc, infoId desc ";
   		  
   	  }else
   	    {
   	     hql =hql +"and auditing="+info.getAuditing()+ "order by  auditing  asc ,infoTime desc,infoId desc  ";
   	     nextPage ="parttyconstructionstudentAffairsAuditing.action?info.auditing=0&" ;
   	     
   	    }
   	  System.out.println("=============  "+where_value);
   	  System.out.println("============= "+where_search);
   	  System.out.println("------------- " +like);
   	  

		   search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
   	  
		   System.out.println("---"+search_parameter);
		   System.out.println("---"+hql);

		   pm = new PageModel();
   	  
   	   System.out.println("hql222---"+hql);
   	   
   	   pm=this.getCommonService().findPage(hql,parem, currentPage, pageSize, nextPage,search_parameter);
   	  
   	   System.out.println("---------------------"+pm.getTotalCount());
    	  
    	  return "parttyconstructionStudentAffairsList2";
      }
      
      /***
       * 
       * 列表自己发布的Info
       * @return
       * @throws Exception
       */
   
        public String ListSelf() throws Exception{
        	
          teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
      	  
      	  String hql =" from Info info where  info.infoType=5 and info.teacherByInfoEditer.teacherId="+teacher.getTeacherId();
      	  
      	  String[] parem = null;
      	  String  search_parameter = "";
  	      String en_where_value="";
       	  
      	  nextPage ="parttyconstructionstudentAffairsListSelf.action" ;
      	  
      	  if(!"".equals(where_value)&&where_value!=null){

 	    	  en_where_value=java.net.URLEncoder.encode(where_value,"utf-8");//URL编码转换
 	    	  
      		  System.out.println("++++-- "+where_value);
      		  if(like.equals("like"))
      		  {
               	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
               	   
               	   
      		  }else{
      			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
      		  }
      		  
      	  }
      	  
      	  //查询条件
      	   search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
      	  
      	  
      	  hql = hql +" order by infoTime desc,infoId desc";
      	  
      	  pm= new PageModel();
      	  
      	  pm=this.getCommonService().findPage(hql, parem, currentPage, pageSize, nextPage,search_parameter);
      	  
      	  return "parttyconstructionStudentAffairsListSelf";
        }
        
        //
        public String ListSelf1() throws Exception{
        	
            teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
        	  
        	  String hql =" from Info info where  info.infoType=5 and info.teacherByInfoEditer.teacherId="+teacher.getTeacherId();
        	  
        	  String[] parem = null;
        	  String  search_parameter = "";
    	      String en_where_value="";
         	  
        	  nextPage ="parttyconstructionstudentAffairsListSelf1.action" ;
        	  
        	  if(!"".equals(where_value)&&where_value!=null){

   	    	  en_where_value=java.net.URLEncoder.encode(where_value,"utf-8");//URL编码转换
   	    	  
        		  System.out.println("++++-- "+where_value);
        		  if(like.equals("like"))
        		  {
                 	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
                 	   
                 	   
        		  }else{
        			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
        		  }
        		  
        	  }
        	  
        	  //查询条件
        	   search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
        	  
        	  
        	  hql = hql +" order by infoTime desc,infoId desc";
        	  
        	  pm= new PageModel();
        	  
        	  pm=this.getCommonService().findPage(hql, parem, currentPage, pageSize, nextPage,search_parameter);
        	  
        	  return "parttyconstructionStudentAffairsListSelf1";
          }
      
      
	/***
	 * 
	 *   Detail  显示单条信息详细
	 */
      
	@SuppressWarnings("unchecked")
	public String Detail() throws Exception{
    	  
    	  String   hql = "from InfoFiles where infoId = "+info.getInfoId();
    
    	  
    	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
    	  //更新阅读次数，加一
    	  info.setInfoReadtimes(info.getInfoReadtimes()+1);
    	  
    	  this.getCommonService().updateObject(info);
    	  
    	  //查找公告相应的附件
    	  infoFiles =this.getCommonService().findListByHql(hql, null);
    	  
    	  return "parttyconstructionStudentAffairsDetail";
      }
	 

	
	
	/***
	 * Auditing 审核通知列表
	 * 
	 */
	  public String Auditing () throws Exception{
		  
		  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		
    	   String hql ="from Info where infoType = 5";
     	   String[] parem =null;
     	   String  search_parameter = "";
     	   String en_where_value="";
     	  
     	  if(!"".equals(where_value)&&where_value!=null){
     		  
     		  en_where_value=java.net.URLEncoder.encode(where_value,"utf-8");//URL编码转换
     		  if(like.equals("like"))
     		  {
     			
     			  System.out.println("fanhuid == "+where_value);
              	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
              	   System.out.println("====++++"+where_value);
              	  
     		  }else{
     			  hql = hql +"and "+where_search+" = '"+where_value+"'";

     		  }
     		  
          	  System.out.println("------------++ "+search_parameter);
     	  }
   
     	  if(info==null||info.getAuditing()==null){
     		  
     		  nextPage ="parttyconstructionstudentAffairsAuditing.action" ;
     	     
     		  
     		  hql =hql + " order by auditing asc ,infoTime desc, infoId desc ";
     		  
     	  }else
     	    {
     	     hql =hql +"and auditing="+info.getAuditing()+ "order by  auditing  asc ,infoTime desc,infoId desc  ";
     	     nextPage ="parttyconstructionstudentAffairsAuditing.action?info.auditing=0&" ;
     	     
     	    }
     	  System.out.println("=============  "+where_value);
     	  System.out.println("============= "+where_search);
     	  System.out.println("------------- " +like);
     	  

 		   search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
     	  
 		   System.out.println("---"+search_parameter);
 		   System.out.println("---"+hql);
  
 		   pm = new PageModel();
     	  
     	   System.out.println("hql222---"+hql);
     	   
     	   pm=this.getCommonService().findPage(hql,parem, currentPage, pageSize, nextPage,search_parameter);
     	  
     	   System.out.println("---------------------"+pm.getTotalCount());
     	   
		  return "parttyconstructionStudentAffairsAuditing";
	  }
	
	
	/***
	 * Inside  院内公布
	 * 
	 */
	  public String Inside() throws Exception{
		  
		  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

    	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  info.setAuditing("1");
    	  
    	  info.setTeacherByInfoAssessor(teacher);
    	  
    	  this.getCommonService().updateObject(info);
    	  
		  
		  return "parttyconstructionStudentAffairsInside";
	  }
	  
	
    /***
     *   Outside 对外公布
     */
    public String Outside() throws Exception{
    	
		  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
    	  
    	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  info.setAuditing("2");
    	  
    	  info.setTeacherByInfoAssessor(teacher);
    	  
    	  this.getCommonService().updateObject(info);
    	  
		  return "parttyconstructionStudentAffairsOutside";
	  }
	
	/***
	 * 
	 *    Notallow 不通过
	 */
    public String Notallow() throws Exception{
		  
      teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
  	  
  	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
  	  //将信息审核为对院内公布 ，即设置auditing为1；
  	  info.setAuditing("3");
  	  
  	  info.setTeacherByInfoAssessor(teacher);
  	  
  	  this.getCommonService().updateObject(info);
  	  
		  return "parttyconstructionStudentAffairsNotallow";
	  }
	
    
    
	
	/***
	 * 
	 *    Rollback 未审核
	 */
    public String Rollback() throws Exception{
    	
    	teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		  
          String   hql = "from InfoFiles where infoId=?";

    	  
    	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  info.setAuditing("0");
    	  
    	  info.setTeacherByInfoAssessor(teacher);
    	  
    	  this.getCommonService().updateObject(info);
    	  
  		  return "parttyconstructionStudentAffairsRollback";
  	  }
    
      /***
       * 非管理员修改
       * @return
       * @throws Exception
       */
    
      public String ModifySelfShow() throws Exception{
   	 
	     teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
	     
	  
	      String   hql = "from InfoFiles file where file.info.infoId= "+info.getInfoId();
	   
	   	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
	       
	  	 if(!info.getTeacherByInfoEditer().getTeacherId().equals(teacher.getTeacherId())){
			
			System.out.println("没有修改的权限");
			
			return "error";
		}
	   	  if(!"0".equals(info.getAuditing())){
	   		  
	   		System.out.println("已通过审核，不能在修改！");
	   		
	   		return "error";
	   	  }
	   	  System.out.println("-------------"+info.getInfoTitle());
	  
	   	  
	   	  //查找公告相应的附件
	   	 infoFiles = this.getCommonService().findListByHql(hql, null);
	   	 Iterator iter = infoFiles.iterator();
	   	 while (iter.hasNext()){
	   		 InfoFiles in = (InfoFiles)iter.next();
	   		 
	   		 System.out.println("infoFiles ----"+ in.getFileName());
	   	 }
	   	 
	 return "parttyconstructionStudentAffairsModifyShowSelf";
}
	
	/***
	 * 
	 *  ModifyShow 修改时显示单体记录
	 */
    
     @SuppressWarnings("unchecked")
	public String ModifyShow() throws Exception{
    	 
    	     teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
    	     
    	  
		      String   hql = "from InfoFiles file where file.info.infoId= "+info.getInfoId();
		   
		   	  info =(Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
		       
		   	  
		   	  //查找公告相应的附件
		   	 infoFiles = this.getCommonService().findListByHql(hql, null);
		   	 Iterator iter = infoFiles.iterator();
		   	 while (iter.hasNext()){
		   		 InfoFiles in = (InfoFiles)iter.next();
		   		 
		   		 System.out.println("infoFiles ----"+ in.getFileName());
		   	 }
		   	 
    	 return "parttyconstructionStudentAffairsModifyShow";
     }
     
     
	
	/***
	 * 
	 *   Modify 修改 
	 *   
	 */
	
    public  String Modify() throws Exception {
    	
    	teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
    	
		List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		setSavePath("");//上传路径
		boolean bl=false;
		boolean b2=false;
		
		
		System.out.println("---Modify---"+info.getInfoTitle());
		System.out.println("---ModifyId---"+info.getInfoId());
		
		Info info1 = (Info)this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
		
		info1.setInfoTitle(info.getInfoTitle());
		info1.setInfoContext(info.getInfoContext());
		info1.setInfoTime(info.getInfoTime());
		info1.setAuditing("0");
		info1.setAssessorInfo(info.getAssessorInfo());
		info1.setInfoSender(info.getInfoSender());
		info1.setTeacherByInfoAssessor(info.getTeacherByInfoAssessor());
		info1.setTeacherByInfoEditer(info.getTeacherByInfoEditer());
		
		//先插入info表
		 //判断信息内容、发布者、标题是否为空，
		if("".equals(info1.getInfoTitle())||"".equals(info1.getInfoContext())||"".equals(info1.getInfoSender())){
			
			return "error";
		}
		 //判断信息内容、发布者、标题是否为空，
		if("".equals(info1.getInfoTitle())||"".equals(info1.getInfoContext())||"".equals(info1.getInfoSender())){
			
			return "error";
		}
		bl= this.getCommonService().updateObject(info1);
		
		//处理文件上传
	     System.out.println("==="+files_content);  
	     System.out.println("=="+files_title);
		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分
				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\affficheFiles\\"+extend_name;//文件在服务器保存的相对路径
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
				InfoFiles afiles=new InfoFiles();
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				afiles.setFileUrl(Save_path);
				afiles.setFileName(files_title[i]);
				afiles.setFileContext(files_content[i]);
				afiles.setInfo(info);	
	
				//在插入附件表
				b2=this.getCommonService().saveObject(afiles);
		
		//文件上传处理结束
		
     	}
		
		if(bl)
		{
			return "parttyconstructionStudentAffairsModify";
		}else{
			return  "error";
		}
    	
    }
    
     /***
	  *   Delete 删除,同时删除附件。
	  */
      public String DeleteSelf() throws Exception{
		  
         teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
   	  
         String hql ="from InfoFiles  infofiles where infofiles.info.infoId ="+info.getInfoId();
         
   	    Info info1 = new Info();
         
   	    info1 =(Info) this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
   	  
   	    
   	    //判断是否可以删除
   	    if(!info1.getTeacherByInfoEditer().getTeacherId().equals(teacher.getTeacherId())){
			
			System.out.println("没有删除的权限");
			
			return "error";
		}
   	    System.out.println("===="+info1.getAuditing());
	   	  if(!"0".equals(info1.getAuditing())){
	   		  
	   		System.out.println("已通过审核，不能再删除！");
	   		
	   		return "error";
	   	  }
   	  
         //删除服务器上的附件
   	  
   	    List fileList = this.getCommonService().findListByHql(hql, null);
   	  
   	    Iterator iter = fileList.iterator();
   	   
   	    while(iter.hasNext()){
   		  
   		  InfoFiles infofiles = (InfoFiles)iter.next();
   		  
   		  String path = ServletActionContext.getRequest().getRealPath("/");
   		  
   		  path = path + infofiles.getFileUrl();
   		  
   		  System.out.println("======="+path);
   		  
   		  this.getCommonService().deleteObject(infofiles);
   		  
   		  this.deleteFile(path);
   		  
   		  System.out.println("===文件删除===");
   		  
   	  }
        
   	  boolean b =this.getCommonService().deleteObject(info1);
   	  if(b){
   		  return "parttyconstructionStudentAffairsDeleteSelf";
   	  }else {
   		  return "error";
   	  }
 	
 	  }
	
	
	 /***
	  *   Delete 删除,同时删除附件。
	  */
      public String Delete() throws Exception{
		  
          teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
    	  
          String hql ="from InfoFiles  infofiles where infofiles.info.infoId ="+info.getInfoId();
          
    	  info =(Info) this.getCommonService().findByIntId(info.getInfoId(), "gdut.jmxywz.been.Info");
    	  
          //删除服务器上的附件
    	  
    	  List fileList = this.getCommonService().findListByHql(hql, null);
    	  
    	  Iterator iter = fileList.iterator();
    	   
    	  while(iter.hasNext()){
    		  
    		  InfoFiles infofiles = (InfoFiles)iter.next();
    		  
    		  String path = ServletActionContext.getRequest().getRealPath("/");
    		  
    		  path = path + infofiles.getFileUrl();
    		  
    		  System.out.println("======="+path);
    		  
    		  this.getCommonService().deleteObject(infofiles);
    		  
    		  this.deleteFile(path);
    		  
    		  System.out.println("===文件删除===");
    		  
    	  }
         
          
    	  boolean b =this.getCommonService().deleteObject(info);
    	  if(b){
    		  return "parttyconstructionStudentAffairsDelete";
    	  }else {
    		  return "error";
    	  }
  	
  	  }
    
       /***
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
		public  boolean deleteFile(String sPath) {   
			boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}  

    
}
	
